<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
</head>

<body>
    <div style="text-align: center;">
        <h1>登录</h1>
        <form id="login-form">
            <label for="username">Username:</label>
            <input type="text" id="username" name="username" value="user" required>
            <br>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password" value="user" required>
            <br>
            <button type="submit">Login</button>
        </form>
    </div>
    <script>
        document.getElementById("login-form").addEventListener("submit", async (event) => {
            event.preventDefault();

            const username = document.getElementById("username").value;
            const password = document.getElementById("password").value;

            const response = await fetch("/login", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({ username, password })
            });

            const data = await response.json();

            if (response.ok) {
                localStorage.setItem("token", data.token);
                window.location.href = "/home";
            } else {
                alert(data.message);
            }
            console.log(data);
        });
    </script>
</body>

</html>